package com.nowcoder.code.lcof;

import com.algorithm.model.ListNode;

import java.util.ArrayList;

public class JZ6 {
    public static void main(String[] args) {
        ListNode listNode = new ListNode();
        ListNode now = listNode;
        now.val=3;
//        now.next = new ListNode(2);
//        now = listNode.next;
//        now.next = new ListNode(1);
        System.out.println(new JZ6().printListFromTailToHead(listNode));

    }

    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        if (listNode == null) {
            return new ArrayList<>(0);
        }
        int i = 0;
        ListNode now = listNode;
        ListNode last = null;
        ListNode next = null;
        while (now != null) {
            next = now.next;
            now.next = last;
            last = now;
            now = next;
            i++;
        }

        ArrayList<Integer> list = new ArrayList<>(i);
        while (last != null) {
            list.add(last.val);
            last = last.next;
        }

        return list;
    }

}
